<!--
  - Copyright (c) 2019.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
  <el-card>
    <el-row style="padding-left: calc(50% - 74px);margin-top: 16px;margin-bottom: 16px;text-align: left;">
      <el-button @click="goBack()"
                 style="padding: 7px 18px; margin-right: 7px; width: 132px; height: 36px;"
                 type="info" round>
        <span style="font-size: 16px;line-height: 20px">返回列表</span>
      </el-button>

    </el-row>
    <el-row>
      <el-col style="text-align: left;color: #909399;">
        <b>移交信息</b>
      </el-col>
    </el-row>
    <el-form inline style="background-color: #F4F8F9;"
             :model='customerTransfer'
             :rules="rules"
             ref='customerTransferRef'>
      <el-row>
        <el-col :md="12" :lg="8">
          <el-form-item label="移交后主办" prop="mainManagerAfterId">
            <ft-transfer-user v-model="customerTransfer.mainManagerAfterId" @change="changeMainDeptId" size="small"/>
          </el-form-item>
        </el-col>
        <el-col :md="12" :lg="8">
          <el-form-item label="移交后协办" prop="viceManagerAfterId">
            <ft-transfer-user v-model="customerTransfer.viceManagerAfterId" @change="changeViceDeptId" size="small"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col style="text-align: right;margin-right:20px;margin-bottom: 10px;padding: 16px 0px">
          <el-button @click="cancel()" size="small">取消</el-button>
          <el-button :disabled="!saveAble" type="primary" @click="save()" size="small">保存</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
</template>

<script>

  import FtTransferUser from "../ft/FtTransferUser";

  export default {
    name: 'TransferAdd',
    components: {FtTransferUser},
    data() {
      return {
        saveAble: true,
        mainDeptId:'',
        viceDeptId:'',
        customerTransfer: {},
        //移交后协办信息
        viceManagerAfter: {},
        rules: {
          mainManagerAfterId: [
            { required: true, message: '请输入移交后主办', trigger: 'blur' },
          ],
          viceManagerAfterId: [
            { required: true, message: '移交后协办', trigger: 'blur' },
          ],
        }
      }
    },
    methods: {
      cancel() {
        this.$emit('returnList', {showType: 'table', customerTransferId: null})
      },
      changeMainDeptId(params){
        if(params){
          this.mainDeptId = params.deptId
        } else { this.mainDeptId = ''}

      },
      changeViceDeptId(params){
        if(params){
          this.viceDeptId = params.deptId
        } else { this.viceDeptId = ''}

      },
      save() {
        this.$refs.customerTransferRef.validate((valid) => {
          if (valid) {
            if(this.mainDeptId == null || this.mainDeptId =='' || this.viceDeptId == null || this.viceDeptId == ''){
              this.$message.error('主协办人部门不能为空')
              return
            }
            if(this.mainDeptId != this.viceDeptId){
              this.$message.error('主协办人必须为同一部门')
              return
            }
            if(this.customerTransfer.mainManagerAfterId == this.customerTransfer.viceManagerAfterId){
              this.$message.error('主协办人不能为同一个人')
              return
            }
            this.saveAble = false
            this.$http.put('/joys-rest/lss-customer/transfer/customer/save',this.customerTransfer).then(resp => {
              this.$message.success('保存成功')
              this.customerTransfer = resp.body
              this.goDetail()

            }, resp => {
              this.$message.error(resp.body.message)
            });
          } else {
            this.saveAble = true
            // this.$message.error("保存失败")
            // console.log('error submit!!');
            return false;
          }
        })
      },
      //返回
      goBack() {
        this.$emit('returnList', {showType: 'table', activeName: this.activeName,customerTransferId:this.customerTransfer.customerTransferId})
      },
      //保存成功跳转详情
      goDetail() {
        this.$emit('returnList', {showType: 'detial', activeName: this.activeName,customerTransferId:this.customerTransfer.customerTransferId})
      }
    },
  }

</script>

<style>

</style>
